package com.hjx.movie.core.system.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjx.movie.core.system.service.admin.ICinemaMovieShowService;
import com.hjx.movie.core.system.service.admin.IMovieService;
import com.hjx.movie.persistence.system.mapper.IvyCinemaMovieShowMapper;
import com.hjx.movie.persistence.system.model.IvyCinemaMovieShow;
import com.hjx.movie.persistence.system.model.IvyMovie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/10 - 17:23
 */
@Service
@Transactional
@Slf4j
@CacheConfig(cacheNames = "IvyMovie:SpringCache:CinemaMovieShowCache",keyGenerator = "cacheKeyGenerator")
public class CinemaMovieShowService extends ServiceImpl<IvyCinemaMovieShowMapper, IvyCinemaMovieShow> implements ICinemaMovieShowService {
    @Resource
    IMovieService movieService;

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinemaMovieShow> findAll() {
        return list();
    }

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinemaMovieShow> findAll(Wrapper<IvyCinemaMovieShow> wrapper) {
        return list(wrapper);
    }

    @Cacheable(unless="#result == null")
    @Override
    public IvyCinemaMovieShow findMovieShowById(Long id) {
        return getById(id);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaMovieShowCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean addMovieShow(IvyCinemaMovieShow movieShow) {
        IvyMovie movie = movieService.getById(movieShow.getMovieId());
        String endTime = timePlus(movieShow.getShowTime(), movie.getTime().longValue());
        log.debug("计算出的结束时间为:{}",endTime);
        movieShow.setStartTime(movieShow.getShowTime());
        movieShow.setEndTime(endTime);
        movieShow.setShowDateTime(LocalDateTime.parse(movieShow.getShowDate()+" "+movieShow.getShowTime()
                ,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return save(movieShow);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaMovieShowCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean updateMovieShow(IvyCinemaMovieShow movieShow) {
        IvyMovie movie = movieService.getById(movieShow.getMovieId());
        String endTime = timePlus(movieShow.getShowTime(), movie.getTime().longValue());
        log.debug("计算出的结束时间为:{}",endTime);
        movieShow.setShowDateTime(LocalDateTime.parse(movieShow.getShowDate()+" "+movieShow.getShowTime()
                ,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        movieShow.setStartTime(movieShow.getShowTime());
        movieShow.setEndTime(endTime);
        return updateById(movieShow);
    }
    @CacheEvict(value = "IvyMovie:SpringCache:CinemaMovieShowCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean deleteMovieShow(Long movieShowId) {
        return removeById(movieShowId);
    }

    public String timePlus(String startTime,Long plusTime){
        LocalTime time = LocalTime.parse(startTime);
        LocalTime plus = time.plus(plusTime, ChronoUnit.MINUTES);
        return plus.format(timeFormatter);
    }

    @Override
    public List<IvyCinemaMovieShow> queryMovieShowLike(String s) {
        List<Long> collect = movieService.queryMovieLike(s)
                .stream().map(IvyMovie::getMovieId).collect(Collectors.toList());
        return lambdaQuery().like(IvyCinemaMovieShow::getMovieShowId,s)
                .or().in(IvyCinemaMovieShow::getMovieId,collect)
                .list();
    }
}
